home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / values.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  40.5 KB  |  1,460 lines

  1. /* Low level packing and unpacking of values for GDB, the GNU Debugger.
  2.    Copyright 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include <string.h>
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "value.h"
  25. #include "gdbcore.h"
  26. #include "frame.h"
  27. #include "command.h"
  28. #include "gdbcmd.h"
  29. #include "target.h"
  30.  
  31. /* Local function prototypes. */
  32.  
  33. static value
  34. value_headof PARAMS ((value, struct type *, struct type *));
  35.  
  36. static void
  37. show_values PARAMS ((char *, int));
  38.  
  39. static void
  40. show_convenience PARAMS ((char *, int));
  41.  
  42. /* The value-history records all the values printed
  43.    by print commands during this session.  Each chunk
  44.    records 60 consecutive values.  The first chunk on
  45.    the chain records the most recent values.
  46.    The total number of values is in value_history_count.  */
  47.  
  48. #define VALUE_HISTORY_CHUNK 60
  49.  
  50. struct value_history_chunk
  51. {
  52.   struct value_history_chunk *next;
  53.   value values[VALUE_HISTORY_CHUNK];
  54. };
  55.  
  56. /* Chain of chunks now in use.  */
  57.  
  58. static struct value_history_chunk *value_history_chain;
  59.  
  60. static int value_history_count;    /* Abs number of last entry stored */
  61.  
  62. /* List of all value objects currently allocated
  63.    (except for those released by calls to release_value)
  64.    This is so they can be freed after each command.  */
  65.  
  66. static value all_values;
  67.  
  68. /* Allocate a  value  that has the correct length for type TYPE.  */
  69.  
  70. value
  71. allocate_value (type)
  72.      struct type *type;
  73. {
  74.   register value val;
  75.  
  76.   check_stub_type (type);
  77.  
  78.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  79.   VALUE_NEXT (val) = all_values;
  80.   all_values = val;
  81.   VALUE_TYPE (val) = type;
  82.   VALUE_LVAL (val) = not_lval;
  83.   VALUE_ADDRESS (val) = 0;
  84.   VALUE_FRAME (val) = 0;
  85.   VALUE_OFFSET (val) = 0;
  86.   VALUE_BITPOS (val) = 0;
  87.   VALUE_BITSIZE (val) = 0;
  88.   VALUE_REPEATED (val) = 0;
  89.   VALUE_REPETITIONS (val) = 0;
  90.   VALUE_REGNO (val) = -1;
  91.   VALUE_LAZY (val) = 0;
  92.   VALUE_OPTIMIZED_OUT (val) = 0;
  93.   return val;
  94. }
  95.  
  96. /* Allocate a  value  that has the correct length
  97.    for COUNT repetitions type TYPE.  */
  98.  
  99. value
  100. allocate_repeat_value (type, count)
  101.      struct type *type;
  102.      int count;
  103. {
  104.   register value val;
  105.  
  106.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  107.   VALUE_NEXT (val) = all_values;
  108.   all_values = val;
  109.   VALUE_TYPE (val) = type;
  110.   VALUE_LVAL (val) = not_lval;
  111.   VALUE_ADDRESS (val) = 0;
  112.   VALUE_FRAME (val) = 0;
  113.   VALUE_OFFSET (val) = 0;
  114.   VALUE_BITPOS (val) = 0;
  115.   VALUE_BITSIZE (val) = 0;
  116.   VALUE_REPEATED (val) = 1;
  117.   VALUE_REPETITIONS (val) = count;
  118.   VALUE_REGNO (val) = -1;
  119.   VALUE_LAZY (val) = 0;
  120.   VALUE_OPTIMIZED_OUT (val) = 0;
  121.   return val;
  122. }
  123.  
  124. /* Return a mark in the value chain.  All values allocated after the
  125.    mark is obtained (except for those released) are subject to being freed
  126.    if a subsequent value_free_to_mark is passed the mark.  */
  127. value
  128. value_mark ()
  129. {
  130.   return all_values;
  131. }
  132.  
  133. /* Free all values allocated since MARK was obtained by value_mark
  134.    (except for those released).  */
  135. void
  136. value_free_to_mark (mark)
  137.      value mark;
  138. {
  139.   value val, next;
  140.  
  141.   for (val = all_values; val && val != mark; val = next)
  142.     {
  143.       next = VALUE_NEXT (val);
  144.       value_free (val);
  145.     }
  146.   all_values = val;
  147. }
  148.  
  149. /* Free all the values that have been allocated (except for those released).
  150.    Called after each command, successful or not.  */
  151.  
  152. void
  153. free_all_values ()
  154. {
  155.   register value val, next;
  156.  
  157.   for (val = all_values; val; val = next)
  158.     {
  159.       next = VALUE_NEXT (val);
  160.       value_free (val);
  161.     }
  162.  
  163.   all_values = 0;
  164. }
  165.  
  166. /* Remove VAL from the chain all_values
  167.    so it will not be freed automatically.  */
  168.  
  169. void
  170. release_value (val)
  171.      register value val;
  172. {
  173.   register value v;
  174.  
  175.   if (all_values == val)
  176.     {
  177.       all_values = val->next;
  178.       return;
  179.     }
  180.  
  181.   for (v = all_values; v; v = v->next)
  182.     {
  183.       if (v->next == val)
  184.     {
  185.       v->next = val->next;
  186.       break;
  187.     }
  188.     }
  189. }
  190.  
  191. /* Return a copy of the value ARG.
  192.    It contains the same contents, for same memory address,
  193.    but it's a different block of storage.  */
  194.  
  195. value
  196. value_copy (arg)
  197.      value arg;
  198. {
  199.   register value val;
  200.   register struct type *type = VALUE_TYPE (arg);
  201.   if (VALUE_REPEATED (arg))
  202.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  203.   else
  204.     val = allocate_value (type);
  205.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  206.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  207.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  208.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  209.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  210.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  211.   VALUE_LAZY (val) = VALUE_LAZY (arg);
  212.   if (!VALUE_LAZY (val))
  213.     {
  214.       bcopy (VALUE_CONTENTS_RAW (arg), VALUE_CONTENTS_RAW (val),
  215.          TYPE_LENGTH (VALUE_TYPE (arg))
  216.          * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  217.     }
  218.   return val;
  219. }
  220.  
  221. /* Access to the value history.  */
  222.  
  223. /* Record a new value in the value history.
  224.    Returns the absolute history index of the entry.
  225.    Result of -1 indicates the value was not saved; otherwise it is the
  226.    value history index of this new item.  */
  227.  
  228. int
  229. record_latest_value (val)
  230.      value val;
  231. {
  232.   int i;
  233.  
  234.   /* Check error now if about to store an invalid float.  We return -1
  235.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  236.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  237.     (void) unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  238.     if (i) return -1;        /* Indicate value not saved in history */
  239.   }
  240.  
  241.   /* Here we treat value_history_count as origin-zero
  242.      and applying to the value being stored now.  */
  243.  
  244.   i = value_history_count % VALUE_HISTORY_CHUNK;
  245.   if (i == 0)
  246.     {
  247.       register struct value_history_chunk *new
  248.     = (struct value_history_chunk *)
  249.       xmalloc (sizeof (struct value_history_chunk));
  250.       bzero (new->values, sizeof new->values);
  251.       new->next = value_history_chain;
  252.       value_history_chain = new;
  253.     }
  254.  
  255.   value_history_chain->values[i] = val;
  256.   release_value (val);
  257.  
  258.   /* Now we regard value_history_count as origin-one
  259.      and applying to the value just stored.  */
  260.  
  261.   return ++value_history_count;
  262. }
  263.  
  264. /* Return a copy of the value in the history with sequence number NUM.  */
  265.  
  266. value
  267. access_value_history (num)
  268.      int num;
  269. {
  270.   register struct value_history_chunk *chunk;
  271.   register int i;
  272.   register int absnum = num;
  273.  
  274.   if (absnum <= 0)
  275.     absnum += value_history_count;
  276.  
  277.   if (absnum <= 0)
  278.     {
  279.       if (num == 0)
  280.     error ("The history is empty.");
  281.       else if (num == 1)
  282.     error ("There is only one value in the history.");
  283.       else
  284.     error ("History does not go back to $$%d.", -num);
  285.     }
  286.   if (absnum > value_history_count)
  287.     error ("History has not yet reached $%d.", absnum);
  288.  
  289.   absnum--;
  290.  
  291.   /* Now absnum is always absolute and origin zero.  */
  292.  
  293.   chunk = value_history_chain;
  294.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  295.        i > 0; i--)
  296.     chunk = chunk->next;
  297.  
  298.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  299. }
  300.  
  301. /* Clear the value history entirely.
  302.    Must be done when new symbol tables are loaded,
  303.    because the type pointers become invalid.  */
  304.  
  305. void
  306. clear_value_history ()
  307. {
  308.   register struct value_history_chunk *next;
  309.   register int i;
  310.   register value val;
  311.  
  312.   while (value_history_chain)
  313.     {
  314.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  315.     if (val = value_history_chain->values[i])
  316.       free ((PTR)val);
  317.       next = value_history_chain->next;
  318.       free ((PTR)value_history_chain);
  319.       value_history_chain = next;
  320.     }
  321.   value_history_count = 0;
  322. }
  323.  
  324. static void
  325. show_values (num_exp, from_tty)
  326.      char *num_exp;
  327.      int from_tty;
  328. {
  329.   register int i;
  330.   register value val;
  331.   static int num = 1;
  332.  
  333.   if (num_exp)
  334.     {
  335.       if (num_exp[0] == '+' && num_exp[1] == '\0')
  336.     /* "info history +" should print from the stored position.  */
  337.     ;
  338.       else
  339.     /* "info history <exp>" should print around value number <exp>.  */
  340.     num = parse_and_eval_address (num_exp) - 5;
  341.     }
  342.   else
  343.     {
  344.       /* "info history" means print the last 10 values.  */
  345.       num = value_history_count - 9;
  346.     }
  347.  
  348.   if (num <= 0)
  349.     num = 1;
  350.  
  351.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  352.     {
  353.       val = access_value_history (i);
  354.       printf_filtered ("$%d = ", i);
  355.       value_print (val, stdout, 0, Val_pretty_default);
  356.       printf_filtered ("\n");
  357.     }
  358.  
  359.   /* The next "info history +" should start after what we just printed.  */
  360.   num += 10;
  361.  
  362.   /* Hitting just return after this command should do the same thing as
  363.      "info history +".  If num_exp is null, this is unnecessary, since
  364.      "info history +" is not useful after "info history".  */
  365.   if (from_tty && num_exp)
  366.     {
  367.       num_exp[0] = '+';
  368.       num_exp[1] = '\0';
  369.     }
  370. }
  371.  
  372. /* Internal variables.  These are variables within the debugger
  373.    that hold values assigned by debugger commands.
  374.    The user refers to them with a '$' prefix
  375.    that does not appear in the variable names stored internally.  */
  376.  
  377. static struct internalvar *internalvars;
  378.  
  379. /* Look up an internal variable with name NAME.  NAME should not
  380.    normally include a dollar sign.
  381.  
  382.    If the specified internal variable does not exist,
  383.    one is created, with a void value.  */
  384.  
  385. struct internalvar *
  386. lookup_internalvar (name)
  387.      char *name;
  388. {
  389.   register struct internalvar *var;
  390.  
  391.   for (var = internalvars; var; var = var->next)
  392.     if (!strcmp (var->name, name))
  393.       return var;
  394.  
  395.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  396.   var->name = concat (name, NULL);
  397.   var->value = allocate_value (builtin_type_void);
  398.   release_value (var->value);
  399.   var->next = internalvars;
  400.   internalvars = var;
  401.   return var;
  402. }
  403.  
  404. value
  405. value_of_internalvar (var)
  406.      struct internalvar *var;
  407. {
  408.   register value val;
  409.  
  410. #ifdef IS_TRAPPED_INTERNALVAR
  411.   if (IS_TRAPPED_INTERNALVAR (var->name))
  412.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  413. #endif 
  414.  
  415.   val = value_copy (var->value);
  416.   if (VALUE_LAZY (val))
  417.     value_fetch_lazy (val);
  418.   VALUE_LVAL (val) = lval_internalvar;
  419.   VALUE_INTERNALVAR (val) = var;
  420.   return val;
  421. }
  422.  
  423. void
  424. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  425.      struct internalvar *var;
  426.      int offset, bitpos, bitsize;
  427.      value newval;
  428. {
  429.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  430.  
  431. #ifdef IS_TRAPPED_INTERNALVAR
  432.   if (IS_TRAPPED_INTERNALVAR (var->name))
  433.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  434. #endif
  435.  
  436.   if (bitsize)
  437.     modify_field (addr, (int) value_as_long (newval),
  438.           bitpos, bitsize);
  439.   else
  440.     bcopy (VALUE_CONTENTS (newval), addr,
  441.        TYPE_LENGTH (VALUE_TYPE (newval)));
  442. }
  443.  
  444. void
  445. set_internalvar (var, val)
  446.      struct internalvar *var;
  447.      value val;
  448. {
  449. #ifdef IS_TRAPPED_INTERNALVAR
  450.   if (IS_TRAPPED_INTERNALVAR (var->name))
  451.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  452. #endif
  453.  
  454.   free ((PTR)var->value);
  455.   var->value = value_copy (val);
  456.   release_value (var->value);
  457. }
  458.  
  459. char *
  460. internalvar_name (var)
  461.      struct internalvar *var;
  462. {
  463.   return var->name;
  464. }
  465.  
  466. /* Free all internalvars.  Done when new symtabs are loaded,
  467.    because that makes the values invalid.  */
  468.  
  469. void
  470. clear_internalvars ()
  471. {
  472.   register struct internalvar *var;
  473.  
  474.   while (internalvars)
  475.     {
  476.       var = internalvars;
  477.       internalvars = var->next;
  478.       free ((PTR)var->name);
  479.       free ((PTR)var->value);
  480.       free ((PTR)var);
  481.     }
  482. }
  483.  
  484. static void
  485. show_convenience (ignore, from_tty)
  486.      char *ignore;
  487.      int from_tty;
  488. {
  489.   register struct internalvar *var;
  490.   int varseen = 0;
  491.  
  492.   for (var = internalvars; var; var = var->next)
  493.     {
  494. #ifdef IS_TRAPPED_INTERNALVAR
  495.       if (IS_TRAPPED_INTERNALVAR (var->name))
  496.     continue;
  497. #endif
  498.       if (!varseen)
  499.     {
  500.       varseen = 1;
  501.     }
  502.       printf_filtered ("$%s = ", var->name);
  503.       value_print (var->value, stdout, 0, Val_pretty_default);
  504.       printf_filtered ("\n");
  505.     }
  506.   if (!varseen)
  507.     printf ("No debugger convenience variables now defined.\n\
  508. Convenience variables have names starting with \"$\";\n\
  509. use \"set\" as in \"set $foo = 5\" to define them.\n");
  510. }
  511.  
  512. /* Extract a value as a C number (either long or double).
  513.    Knows how to convert fixed values to double, or
  514.    floating values to long.
  515.    Does not deallocate the value.  */
  516.  
  517. LONGEST
  518. value_as_long (val)
  519.      register value val;
  520. {
  521.   /* This coerces arrays and functions, which is necessary (e.g.
  522.      in disassemble_command).  It also dereferences references, which
  523.      I suspect is the most logical thing to do.  */
  524.   if (TYPE_CODE (VALUE_TYPE (val)) != TYPE_CODE_ENUM)
  525.     COERCE_ARRAY (val);
  526.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  527. }
  528.  
  529. double
  530. value_as_double (val)
  531.      register value val;
  532. {
  533.   double foo;
  534.   int inv;
  535.   
  536.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  537.   if (inv)
  538.     error ("Invalid floating value found in program.");
  539.   return foo;
  540. }
  541. /* Extract a value as a C pointer.
  542.    Does not deallocate the value.  */
  543. CORE_ADDR
  544. value_as_pointer (val)
  545.      value val;
  546. {
  547.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  548.      whether we want this to be true eventually.  */
  549.   return value_as_long (val);
  550. }
  551.  
  552. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  553.    as a long, or as a double, assuming the raw data is described
  554.    by type TYPE.  Knows how to convert different sizes of values
  555.    and can convert between fixed and floating point.  We don't assume
  556.    any alignment for the raw data.  Return value is in host byte order.
  557.  
  558.    If you want functions and arrays to be coerced to pointers, and
  559.    references to be dereferenced, call value_as_long() instead.
  560.  
  561.    C++: It is assumed that the front-end has taken care of
  562.    all matters concerning pointers to members.  A pointer
  563.    to member which reaches here is considered to be equivalent
  564.    to an INT (or some size).  After all, it is only an offset.  */
  565.  
  566. /* FIXME:  This should be rewritten as a switch statement for speed and
  567.    ease of comprehension.  */
  568.  
  569. LONGEST
  570. unpack_long (type, valaddr)
  571.      struct type *type;
  572.      char *valaddr;
  573. {
  574.   register enum type_code code = TYPE_CODE (type);
  575.   register int len = TYPE_LENGTH (type);
  576.   register int nosign = TYPE_UNSIGNED (type);
  577.  
  578.   if (code == TYPE_CODE_ENUM || code == TYPE_CODE_BOOL)
  579.     code = TYPE_CODE_INT;
  580.   if (code == TYPE_CODE_FLT)
  581.     {
  582.       if (len == sizeof (float))
  583.     {
  584.       float retval;
  585.       bcopy (valaddr, &retval, sizeof (retval));
  586.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  587.       return retval;
  588.     }
  589.  
  590.       if (len == sizeof (double))
  591.     {
  592.       double retval;
  593.       bcopy (valaddr, &retval, sizeof (retval));
  594.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  595.       return retval;
  596.     }
  597.       else
  598.     {
  599.       error ("Unexpected type of floating point number.");
  600.     }
  601.     }
  602.   else if (code == TYPE_CODE_INT && nosign)
  603.     {
  604.       if (len == sizeof (char))
  605.     {
  606.       unsigned char retval = * (unsigned char *) valaddr;
  607.       /* SWAP_TARGET_AND_HOST (&retval, sizeof (unsigned char)); */
  608.       return retval;
  609.     }
  610.  
  611.       if (len == sizeof (short))
  612.     {
  613.       unsigned short retval;
  614.       bcopy (valaddr, &retval, sizeof (retval));
  615.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  616.       return retval;
  617.     }
  618.  
  619.       if (len == sizeof (int))
  620.     {
  621.       unsigned int retval;
  622.       bcopy (valaddr, &retval, sizeof (retval));
  623.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  624.       return retval;
  625.     }
  626.  
  627.       if (len == sizeof (long))
  628.     {
  629.       unsigned long retval;
  630.       bcopy (valaddr, &retval, sizeof (retval));
  631.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  632.       return retval;
  633.     }
  634. #ifdef LONG_LONG
  635.       if (len == sizeof (long long))
  636.     {
  637.       unsigned long long retval;
  638.       bcopy (valaddr, &retval, sizeof (retval));
  639.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  640.       return retval;
  641.     }
  642. #endif
  643.       else
  644.     {
  645.       error ("That operation is not possible on an integer of that size.");
  646.     }
  647.     }
  648.   else if (code == TYPE_CODE_INT)
  649.     {
  650.       if (len == sizeof (char))
  651.     {
  652.       SIGNED char retval;    /* plain chars might be unsigned on host */
  653.       bcopy (valaddr, &retval, sizeof (retval));
  654.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  655.       return retval;
  656.     }
  657.  
  658.       if (len == sizeof (short))
  659.     {
  660.       short retval;
  661.       bcopy (valaddr, &retval, sizeof (retval));
  662.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  663.       return retval;
  664.     }
  665.  
  666.       if (len == sizeof (int))
  667.     {
  668.       int retval;
  669.       bcopy (valaddr, &retval, sizeof (retval));
  670.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  671.       return retval;
  672.     }
  673.  
  674.       if (len == sizeof (long))
  675.     {
  676.       long retval;
  677.       bcopy (valaddr, &retval, sizeof (retval));
  678.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  679.       return retval;
  680.     }
  681.  
  682. #ifdef LONG_LONG
  683.       if (len == sizeof (long long))
  684.     {
  685.       long long retval;
  686.       bcopy (valaddr, &retval, sizeof (retval));
  687.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  688.       return retval;
  689.     }
  690. #endif
  691.       else
  692.     {
  693.       error ("That operation is not possible on an integer of that size.");
  694.     }
  695.     }
  696.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  697.      whether we want this to be true eventually.  */
  698.   else if (code == TYPE_CODE_PTR
  699.        || code == TYPE_CODE_REF)
  700.     {
  701.       if (len == sizeof(long))
  702.       {
  703.     long retval;
  704.     bcopy (valaddr, &retval, sizeof(retval));
  705.     SWAP_TARGET_AND_HOST (&retval, sizeof(retval));
  706.     return retval;
  707.       }
  708.       else if (len == sizeof(short))
  709.       {
  710.     short retval;
  711.     bcopy (valaddr, &retval, len);
  712.     SWAP_TARGET_AND_HOST (&retval, len);
  713.     return retval;
  714.       }
  715.     }
  716.   else if (code == TYPE_CODE_MEMBER)
  717.     error ("not implemented: member types in unpack_long");
  718.   else if (code == TYPE_CODE_CHAR)
  719.     return *(unsigned char *)valaddr;
  720.  
  721.   error ("Value not integer or pointer.");
  722.   return 0;     /* For lint -- never reached */
  723. }
  724.  
  725. /* Return a double value from the specified type and address.
  726.    INVP points to an int which is set to 0 for valid value,
  727.    1 for invalid value (bad float format).  In either case,
  728.    the returned double is OK to use.  Argument is in target
  729.    format, result is in host format.  */
  730.  
  731. double
  732. unpack_double (type, valaddr, invp)
  733.      struct type *type;
  734.      char *valaddr;
  735.      int *invp;
  736. {
  737.   register enum type_code code = TYPE_CODE (type);
  738.   register int len = TYPE_LENGTH (type);
  739.   register int nosign = TYPE_UNSIGNED (type);
  740.  
  741.   *invp = 0;            /* Assume valid.   */
  742.   if (code == TYPE_CODE_FLT)
  743.     {
  744.       if (INVALID_FLOAT (valaddr, len))
  745.     {
  746.       *invp = 1;
  747.       return 1.234567891011121314;
  748.     }
  749.  
  750.       if (len == sizeof (float))
  751.     {
  752.       float retval;
  753.       bcopy (valaddr, &retval, sizeof (retval));
  754.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  755.       return retval;
  756.     }
  757.  
  758.       if (len == sizeof (double))
  759.     {
  760.       double retval;
  761.       bcopy (valaddr, &retval, sizeof (retval));
  762.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  763.       return retval;
  764.     }
  765.       else
  766.     {
  767.       error ("Unexpected type of floating point number.");
  768.       return 0; /* Placate lint.  */
  769.     }
  770.     }
  771.   else if (nosign) {
  772.    /* Unsigned -- be sure we compensate for signed LONGEST.  */
  773. #ifdef LONG_LONG
  774.    return (unsigned long long) unpack_long (type, valaddr);
  775. #else
  776.    return (unsigned long     ) unpack_long (type, valaddr);
  777. #endif
  778.   } else {
  779.     /* Signed -- we are OK with unpack_long.  */
  780.     return unpack_long (type, valaddr);
  781.   }
  782. }
  783.  
  784. /* Unpack raw data (copied from debugee, target byte order) at VALADDR
  785.    as a CORE_ADDR, assuming the raw data is described by type TYPE.
  786.    We don't assume any alignment for the raw data.  Return value is in
  787.    host byte order.
  788.  
  789.    If you want functions and arrays to be coerced to pointers, and
  790.    references to be dereferenced, call value_as_pointer() instead.
  791.  
  792.    C++: It is assumed that the front-end has taken care of
  793.    all matters concerning pointers to members.  A pointer
  794.    to member which reaches here is considered to be equivalent
  795.    to an INT (or some size).  After all, it is only an offset.  */
  796.  
  797. CORE_ADDR
  798. unpack_pointer (type, valaddr)
  799.      struct type *type;
  800.      char *valaddr;
  801. {
  802. #if 0
  803.   /* The user should be able to use an int (e.g. 0x7892) in contexts
  804.      where a pointer is expected.  So this doesn't do enough.  */
  805.   register enum type_code code = TYPE_CODE (type);
  806.   register int len = TYPE_LENGTH (type);
  807.  
  808.   if (code == TYPE_CODE_PTR
  809.       || code == TYPE_CODE_REF)
  810.     {
  811.       if (len == sizeof (CORE_ADDR))
  812.     {
  813.       CORE_ADDR retval;
  814.       bcopy (valaddr, &retval, sizeof (retval));
  815.       SWAP_TARGET_AND_HOST (&retval, sizeof (retval));
  816.       return retval;
  817.     }
  818.       error ("Unrecognized pointer size.");
  819.     }
  820.   else if (code == TYPE_CODE_MEMBER)
  821.     error ("not implemented: member types in unpack_pointer");
  822.  
  823.   error ("Value is not a pointer.");
  824.   return 0;     /* For lint -- never reached */
  825. #else
  826.   /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
  827.      whether we want this to be true eventually.  */
  828.   return unpack_long (type, valaddr);
  829. #endif
  830. }
  831.  
  832. /* Given a value ARG1 (offset by OFFSET bytes)
  833.    of a struct or union type ARG_TYPE,
  834.    extract and return the value of one of its fields.
  835.    FIELDNO says which field.
  836.  
  837.    For C++, must also be able to return values from static fields */
  838.  
  839. value
  840. value_primitive_field (arg1, offset, fieldno, arg_type)
  841.      register value arg1;
  842.      int offset;
  843.      register int fieldno;
  844.      register struct type *arg_type;
  845. {
  846.   register value v;
  847.   register struct type *type;
  848.  
  849.   check_stub_type (arg_type);
  850.   type = TYPE_FIELD_TYPE (arg_type, fieldno);
  851.  
  852.   /* Handle packed fields */
  853.  
  854.   offset += TYPE_FIELD_BITPOS (arg_type, fieldno) / 8;
  855.   if (TYPE_FIELD_BITSIZE (arg_type, fieldno))
  856.     {
  857.       v = value_from_longest (type,
  858.                unpack_field_as_long (arg_type,
  859.                          VALUE_CONTENTS (arg1),
  860.                          fieldno));
  861.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (arg_type, fieldno) % 8;
  862.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (arg_type, fieldno);
  863.     }
  864.   else
  865.     {
  866.       v = allocate_value (type);
  867.       if (VALUE_LAZY (arg1))
  868.     VALUE_LAZY (v) = 1;
  869.       else
  870.     bcopy (VALUE_CONTENTS_RAW (arg1) + offset,
  871.            VALUE_CONTENTS_RAW (v),
  872.            TYPE_LENGTH (type));
  873.     }
  874.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  875.   if (VALUE_LVAL (arg1) == lval_internalvar)
  876.     VALUE_LVAL (v) = lval_internalvar_component;
  877.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  878.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  879.   return v;
  880. }
  881.  
  882. /* Given a value ARG1 of a struct or union type,
  883.    extract and return the value of one of its fields.
  884.    FIELDNO says which field.
  885.  
  886.    For C++, must also be able to return values from static fields */
  887.  
  888. value
  889. value_field (arg1, fieldno)
  890.      register value arg1;
  891.      register int fieldno;
  892. {
  893.   return value_primitive_field (arg1, 0, fieldno, VALUE_TYPE (arg1));
  894. }
  895.  
  896. /* Return a non-virtual function as a value.
  897.    F is the list of member functions which contains the desired method.
  898.    J is an index into F which provides the desired method. */
  899.  
  900. value
  901. value_fn_field (f, j)
  902.      struct fn_field *f;
  903.      int j;
  904. {
  905.   register value v;
  906.   register struct type *type = TYPE_FN_FIELD_TYPE (f, j);
  907.   struct symbol *sym;
  908.  
  909.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
  910.                0, VAR_NAMESPACE, 0, NULL);
  911.   if (! sym) error ("Internal error: could not find physical method named %s",
  912.             TYPE_FN_FIELD_PHYSNAME (f, j));
  913.   
  914.   v = allocate_value (type);
  915.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  916.   VALUE_TYPE (v) = type;
  917.   return v;
  918. }
  919.  
  920. /* Return a virtual function as a value.
  921.    ARG1 is the object which provides the virtual function
  922.    table pointer.  ARG1 is side-effected in calling this function.
  923.    F is the list of member functions which contains the desired virtual
  924.    function.
  925.    J is an index into F which provides the desired virtual function.
  926.  
  927.    TYPE is the type in which F is located.  */
  928. value
  929. value_virtual_fn_field (arg1, f, j, type)
  930.      value arg1;
  931.      struct fn_field *f;
  932.      int j;
  933.      struct type *type;
  934. {
  935.   /* First, get the virtual function table pointer.  That comes
  936.      with a strange type, so cast it to type `pointer to long' (which
  937.      should serve just fine as a function type).  Then, index into
  938.      the table, and convert final value to appropriate function type.  */
  939.   value entry, vfn, vtbl;
  940.   value vi = value_from_longest (builtin_type_int, 
  941.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  942.   struct type *fcontext = TYPE_FN_FIELD_FCONTEXT (f, j);
  943.   struct type *context;
  944.   if (fcontext == NULL)
  945.    /* We don't have an fcontext (e.g. the program was compiled with
  946.       g++ version 1).  Try to get the vtbl from the TYPE_VPTR_BASETYPE.
  947.       This won't work right for multiple inheritance, but at least we
  948.       should do as well as GDB 3.x did.  */
  949.     fcontext = TYPE_VPTR_BASETYPE (type);
  950.   context = lookup_pointer_type (fcontext);
  951.   /* Now context is a pointer to the basetype containing the vtbl.  */
  952.   if (TYPE_TARGET_TYPE (context) != VALUE_TYPE (arg1))
  953.     arg1 = value_ind (value_cast (context, value_addr (arg1)));
  954.  
  955.   context = VALUE_TYPE (arg1);
  956.   /* Now context is the basetype containing the vtbl.  */
  957.  
  958.   /* This type may have been defined before its virtual function table
  959.      was.  If so, fill in the virtual function table entry for the
  960.      type now.  */
  961.   if (TYPE_VPTR_FIELDNO (context) < 0)
  962.     fill_in_vptr_fieldno (context);
  963.  
  964.   /* The virtual function table is now an array of structures
  965.      which have the form { int16 offset, delta; void *pfn; }.  */
  966.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (context)));
  967.  
  968.   /* Index into the virtual function table.  This is hard-coded because
  969.      looking up a field is not cheap, and it may be important to save
  970.      time, e.g. if the user has set a conditional breakpoint calling
  971.      a virtual function.  */
  972.   entry = value_subscript (vtbl, vi);
  973.  
  974.   /* Move the `this' pointer according to the virtual function table.  */
  975.   VALUE_OFFSET (arg1) += value_as_long (value_field (entry, 0));
  976.   if (! VALUE_LAZY (arg1))
  977.     {
  978.       VALUE_LAZY (arg1) = 1;
  979.       value_fetch_lazy (arg1);
  980.     }
  981.  
  982.   vfn = value_field (entry, 2);
  983.   /* Reinstantiate the function pointer with the correct type.  */
  984.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  985.  
  986.   return vfn;
  987. }
  988.  
  989. /* ARG is a pointer to an object we know to be at least
  990.    a DTYPE.  BTYPE is the most derived basetype that has
  991.    already been searched (and need not be searched again).
  992.    After looking at the vtables between BTYPE and DTYPE,
  993.    return the most derived type we find.  The caller must
  994.    be satisfied when the return value == DTYPE.
  995.  
  996.    FIXME-tiemann: should work with dossier entries as well.  */
  997.  
  998. static value
  999. value_headof (arg, btype, dtype)
  1000.      value arg;
  1001.      struct type *btype, *dtype;
  1002. {
  1003.   /* First collect the vtables we must look at for this object.  */
  1004.   /* FIXME-tiemann: right now, just look at top-most vtable.  */
  1005.   value vtbl, entry, best_entry = 0;
  1006.   /* FIXME: entry_type is never used.  */
  1007.   struct type *entry_type;
  1008.   int i, nelems;
  1009.   int offset, best_offset = 0;
  1010.   struct symbol *sym;
  1011.   CORE_ADDR pc_for_sym;
  1012.   char *demangled_name;
  1013.   struct minimal_symbol *msymbol;
  1014.  
  1015.   btype = TYPE_VPTR_BASETYPE (dtype);
  1016.   check_stub_type (btype);
  1017.   if (btype != dtype)
  1018.     vtbl = value_cast (lookup_pointer_type (btype), arg);
  1019.   else
  1020.     vtbl = arg;
  1021.   vtbl = value_ind (value_field (value_ind (vtbl), TYPE_VPTR_FIELDNO (btype)));
  1022.  
  1023.   /* Check that VTBL looks like it points to a virtual function table.  */
  1024.   msymbol = lookup_minimal_symbol_by_pc (VALUE_ADDRESS (vtbl));
  1025.   if (msymbol == NULL
  1026.       || !VTBL_PREFIX_P (demangled_name = msymbol -> name))
  1027.     {
  1028.       /* If we expected to find a vtable, but did not, let the user
  1029.      know that we aren't happy, but don't throw an error.
  1030.      FIXME: there has to be a better way to do this.  */
  1031.       struct type *error_type = (struct type *)xmalloc (sizeof (struct type));
  1032.       bcopy (VALUE_TYPE (arg), error_type, sizeof (struct type));
  1033.       TYPE_NAME (error_type) = savestring ("suspicious *", sizeof ("suspicious *"));
  1034.       VALUE_TYPE (arg) = error_type;
  1035.       return arg;
  1036.     }
  1037.  
  1038.   /* Now search through the virtual function table.  */
  1039.   entry = value_ind (vtbl);
  1040.   nelems = longest_to_int (value_as_long (value_field (entry, 2)));
  1041.   for (i = 1; i <= nelems; i++)
  1042.     {
  1043.       entry = value_subscript (vtbl, value_from_longest (builtin_type_int, 
  1044.                               (LONGEST) i));
  1045.       offset = longest_to_int (value_as_long (value_field (entry, 0)));
  1046.       /* If we use '<=' we can handle single inheritance
  1047.        * where all offsets are zero - just use the first entry found. */
  1048.       if (offset <= best_offset)
  1049.     {
  1050.       best_offset = offset;
  1051.       best_entry = entry;
  1052.     }
  1053.     }
  1054.   /* Move the pointer according to BEST_ENTRY's offset, and figure
  1055.      out what type we should return as the new pointer.  */
  1056.   if (best_entry == 0)
  1057.     {
  1058.       /* An alternative method (which should no longer be necessary).
  1059.        * But we leave it in for future use, when we will hopefully
  1060.        * have optimizes the vtable to use thunks instead of offsets. */
  1061.       /* Use the name of vtable itself to extract a base type. */
  1062.       demangled_name += 4;  /* Skip _vt$ prefix. */
  1063.     }
  1064.   else
  1065.     {
  1066.       pc_for_sym = value_as_pointer (value_field (best_entry, 2));
  1067.       sym = find_pc_function (pc_for_sym);
  1068.       demangled_name = cplus_demangle (SYMBOL_NAME (sym), -1);
  1069.       *(strchr (demangled_name, ':')) = '\0';
  1070.     }
  1071.   sym = lookup_symbol (demangled_name, 0, VAR_NAMESPACE, 0, 0);
  1072.   if (sym == 0)
  1073.     error ("could not find type declaration for `%s'", SYMBOL_NAME (sym));
  1074.   if (best_entry)
  1075.     {
  1076.       free (demangled_name);
  1077.       arg = value_add (value_cast (builtin_type_int, arg),
  1078.                value_field (best_entry, 0));
  1079.     }
  1080.   VALUE_TYPE (arg) = lookup_pointer_type (SYMBOL_TYPE (sym));
  1081.   return arg;
  1082. }
  1083.  
  1084. /* ARG is a pointer object of type TYPE.  If TYPE has virtual
  1085.    function tables, probe ARG's tables (including the vtables
  1086.    of its baseclasses) to figure out the most derived type that ARG
  1087.    could actually be a pointer to.  */
  1088.  
  1089. value
  1090. value_from_vtable_info (arg, type)
  1091.      value arg;
  1092.      struct type *type;
  1093. {
  1094.   /* Take care of preliminaries.  */
  1095.   if (TYPE_VPTR_FIELDNO (type) < 0)
  1096.     fill_in_vptr_fieldno (type);
  1097.   if (TYPE_VPTR_FIELDNO (type) < 0 || VALUE_REPEATED (arg))
  1098.     return 0;
  1099.  
  1100.   return value_headof (arg, 0, type);
  1101. }
  1102.  
  1103. /* Compute the address of the baseclass which is
  1104.    the INDEXth baseclass of class TYPE.  The TYPE base
  1105.    of the object is at VALADDR.
  1106.  
  1107.    If ERRP is non-NULL, set *ERRP to be the errno code of any error,
  1108.    or 0 if no error.  In that case the return value is not the address
  1109.    of the baseclasss, but the address which could not be read
  1110.    successfully.  */
  1111.  
  1112. char *
  1113. baseclass_addr (type, index, valaddr, valuep, errp)
  1114.      struct type *type;
  1115.      int index;
  1116.      char *valaddr;
  1117.      value *valuep;
  1118.      int *errp;
  1119. {
  1120.   struct type *basetype = TYPE_BASECLASS (type, index);
  1121.  
  1122.   if (errp)
  1123.     *errp = 0;
  1124.  
  1125.   if (BASETYPE_VIA_VIRTUAL (type, index))
  1126.     {
  1127.       /* Must hunt for the pointer to this virtual baseclass.  */
  1128.       register int i, len = TYPE_NFIELDS (type);
  1129.       register int n_baseclasses = TYPE_N_BASECLASSES (type);
  1130.       char *vbase_name, *type_name = type_name_no_tag (basetype);
  1131.  
  1132.       vbase_name = (char *)alloca (strlen (type_name) + 8);
  1133.       sprintf (vbase_name, "_vb$%s", type_name);
  1134.       /* First look for the virtual baseclass pointer
  1135.      in the fields.  */
  1136.       for (i = n_baseclasses; i < len; i++)
  1137.     {
  1138.       if (! strcmp (vbase_name, TYPE_FIELD_NAME (type, i)))
  1139.         {
  1140.           value val = allocate_value (basetype);
  1141.           CORE_ADDR addr;
  1142.           int status;
  1143.  
  1144.           addr
  1145.         = unpack_pointer (TYPE_FIELD_TYPE (type, i),
  1146.                   valaddr + (TYPE_FIELD_BITPOS (type, i) / 8));
  1147.  
  1148.           status = target_read_memory (addr,
  1149.                        VALUE_CONTENTS_RAW (val),
  1150.                        TYPE_LENGTH (basetype));
  1151.           VALUE_LVAL (val) = lval_memory;
  1152.           VALUE_ADDRESS (val) = addr;
  1153.  
  1154.           if (status != 0)
  1155.         {
  1156.           if (valuep)
  1157.             *valuep = NULL;
  1158.           release_value (val);
  1159.           value_free (val);
  1160.           if (errp)
  1161.             *errp = status;
  1162.           return (char *)addr;
  1163.         }
  1164.           else
  1165.         {
  1166.           if (valuep)
  1167.             *valuep = val;
  1168.           return (char *) VALUE_CONTENTS (val);
  1169.         }
  1170.         }
  1171.     }
  1172.       /* Not in the fields, so try looking through the baseclasses.  */
  1173.       for (i = index+1; i < n_baseclasses; i++)
  1174.     {
  1175.       char *baddr;
  1176.  
  1177.       baddr = baseclass_addr (type, i, valaddr, valuep, errp);
  1178.       if (baddr)
  1179.         return baddr;
  1180.     }
  1181.       /* Not found.  */
  1182.       if (valuep)
  1183.     *valuep = 0;
  1184.       return 0;
  1185.     }
  1186.  
  1187.   /* Baseclass is easily computed.  */
  1188.   if (valuep)
  1189.     *valuep = 0;
  1190.   return valaddr + TYPE_BASECLASS_BITPOS (type, index) / 8;
  1191. }
  1192.  
  1193. long
  1194. unpack_field_as_long (type, valaddr, fieldno)
  1195.      struct type *type;
  1196.      char *valaddr;
  1197.      int fieldno;
  1198. {
  1199.   unsigned long val;
  1200.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  1201.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  1202.  
  1203.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  1204.   SWAP_TARGET_AND_HOST (&val, sizeof val);
  1205.  
  1206.   /* Extracting bits depends on endianness of the machine.  */
  1207. #if BITS_BIG_ENDIAN
  1208.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  1209. #else
  1210.   val = val >> (bitpos % 8);
  1211. #endif
  1212.  
  1213.   if (bitsize < 8 * sizeof (val))
  1214.     val &= (((unsigned long)1) << bitsize) - 1;
  1215.   return val;
  1216. }
  1217.  
  1218. /* Modify the value of a bitfield.  ADDR points to a block of memory in
  1219.    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
  1220.    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
  1221.    indicate which bits (in target bit order) comprise the bitfield.  */
  1222.  
  1223. void
  1224. modify_field (addr, fieldval, bitpos, bitsize)
  1225.      char *addr;
  1226.      int fieldval;
  1227.      int bitpos, bitsize;
  1228. {
  1229.   long oword;
  1230.  
  1231.   /* Reject values too big to fit in the field in question,
  1232.      otherwise adjoining fields may be corrupted.  */
  1233.   if (bitsize < (8 * sizeof (fieldval))
  1234.       && 0 != (fieldval & ~((1<<bitsize)-1)))
  1235.     error ("Value %d does not fit in %d bits.", fieldval, bitsize);
  1236.   
  1237.   bcopy (addr, &oword, sizeof oword);
  1238.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To host format */
  1239.  
  1240.   /* Shifting for bit field depends on endianness of the target machine.  */
  1241. #if BITS_BIG_ENDIAN
  1242.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  1243. #endif
  1244.  
  1245.   /* Mask out old value, while avoiding shifts >= longword size */
  1246.   if (bitsize < 8 * sizeof (oword))
  1247.     oword &= ~(((((unsigned long)1) << bitsize) - 1) << bitpos);
  1248.   else
  1249.     oword &= ~((-1) << bitpos);
  1250.   oword |= fieldval << bitpos;
  1251.  
  1252.   SWAP_TARGET_AND_HOST (&oword, sizeof oword);        /* To target format */
  1253.   bcopy (&oword, addr, sizeof oword);
  1254. }
  1255.  
  1256. /* Convert C numbers into newly allocated values */
  1257.  
  1258. value
  1259. value_from_longest (type, num)
  1260.      struct type *type;
  1261.      register LONGEST num;
  1262. {
  1263.   register value val = allocate_value (type);
  1264.   register enum type_code code = TYPE_CODE (type);
  1265.   register int len = TYPE_LENGTH (type);
  1266.  
  1267.   /* FIXME, we assume that pointers have the same form and byte order as
  1268.      integers, and that all pointers have the same form.  */
  1269.   if (code == TYPE_CODE_INT  || code == TYPE_CODE_ENUM || 
  1270.       code == TYPE_CODE_CHAR || code == TYPE_CODE_PTR ||
  1271.       code == TYPE_CODE_REF)
  1272.     {
  1273.       if (len == sizeof (char))
  1274.     * (char *) VALUE_CONTENTS_RAW (val) = num;
  1275.       else if (len == sizeof (short))
  1276.     * (short *) VALUE_CONTENTS_RAW (val) = num;
  1277.       else if (len == sizeof (int))
  1278.     * (int *) VALUE_CONTENTS_RAW (val) = num;
  1279.       else if (len == sizeof (long))
  1280.     * (long *) VALUE_CONTENTS_RAW (val) = num;
  1281. #ifdef LONG_LONG
  1282.       else if (len == sizeof (long long))
  1283.     * (long long *) VALUE_CONTENTS_RAW (val) = num;
  1284. #endif
  1285.       else
  1286.     error ("Integer type encountered with unexpected data length.");
  1287.     }
  1288.   else
  1289.     error ("Unexpected type encountered for integer constant.");
  1290.  
  1291.   /* num was in host byte order.  So now put the value's contents
  1292.      into target byte order.  */
  1293.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1294.  
  1295.   return val;
  1296. }
  1297.  
  1298. value
  1299. value_from_double (type, num)
  1300.      struct type *type;
  1301.      double num;
  1302. {
  1303.   register value val = allocate_value (type);
  1304.   register enum type_code code = TYPE_CODE (type);
  1305.   register int len = TYPE_LENGTH (type);
  1306.  
  1307.   if (code == TYPE_CODE_FLT)
  1308.     {
  1309.       if (len == sizeof (float))
  1310.     * (float *) VALUE_CONTENTS_RAW (val) = num;
  1311.       else if (len == sizeof (double))
  1312.     * (double *) VALUE_CONTENTS_RAW (val) = num;
  1313.       else
  1314.     error ("Floating type encountered with unexpected data length.");
  1315.     }
  1316.   else
  1317.     error ("Unexpected type encountered for floating constant.");
  1318.  
  1319.   /* num was in host byte order.  So now put the value's contents
  1320.      into target byte order.  */
  1321.   SWAP_TARGET_AND_HOST (VALUE_CONTENTS_RAW (val), len);
  1322.  
  1323.   return val;
  1324. }
  1325.  
  1326. /* Deal with the value that is "about to be returned".  */
  1327.  
  1328. /* Return the value that a function returning now
  1329.    would be returning to its caller, assuming its type is VALTYPE.
  1330.    RETBUF is where we look for what ought to be the contents
  1331.    of the registers (in raw form).  This is because it is often
  1332.    desirable to restore old values to those registers
  1333.    after saving the contents of interest, and then call
  1334.    this function using the saved values.
  1335.    struct_return is non-zero when the function in question is
  1336.    using the structure return conventions on the machine in question;
  1337.    0 when it is using the value returning conventions (this often
  1338.    means returning pointer to where structure is vs. returning value). */
  1339.  
  1340. value
  1341. value_being_returned (valtype, retbuf, struct_return)
  1342.      register struct type *valtype;
  1343.      char retbuf[REGISTER_BYTES];
  1344.      int struct_return;
  1345.      /*ARGSUSED*/
  1346. {
  1347.   register value val;
  1348.   CORE_ADDR addr;
  1349.  
  1350. #if defined (EXTRACT_STRUCT_VALUE_ADDRESS)
  1351.   /* If this is not defined, just use EXTRACT_RETURN_VALUE instead.  */
  1352.   if (struct_return) {
  1353.     addr = EXTRACT_STRUCT_VALUE_ADDRESS (retbuf);
  1354.     if (!addr)
  1355.       error ("Function return value unknown");
  1356.     return value_at (valtype, addr);
  1357.   }
  1358. #endif
  1359.  
  1360.   val = allocate_value (valtype);
  1361.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS_RAW (val));
  1362.  
  1363.   return val;
  1364. }
  1365.  
  1366. /* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
  1367.    EXTRACT_RETURN_VALUE?  GCC_P is true if compiled with gcc
  1368.    and TYPE is the type (which is known to be struct, union or array).
  1369.  
  1370.    On most machines, the struct convention is used unless we are
  1371.    using gcc and the type is of a special size.  */
  1372. #if !defined (USE_STRUCT_CONVENTION)
  1373. #define USE_STRUCT_CONVENTION(gcc_p, type)\
  1374.   (!((gcc_p) && (TYPE_LENGTH (value_type) == 1                \
  1375.          || TYPE_LENGTH (value_type) == 2             \
  1376.              || TYPE_LENGTH (value_type) == 4             \
  1377.          || TYPE_LENGTH (value_type) == 8             \
  1378.          )                                            \
  1379.      ))
  1380. #endif
  1381.  
  1382. /* Return true if the function specified is using the structure returning
  1383.    convention on this machine to return arguments, or 0 if it is using
  1384.    the value returning convention.  FUNCTION is the value representing
  1385.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  1386.    is the type returned by the function.  GCC_P is nonzero if compiled
  1387.    with GCC.  */
  1388.  
  1389. int
  1390. using_struct_return (function, funcaddr, value_type, gcc_p)
  1391.      value function;
  1392.      CORE_ADDR funcaddr;
  1393.      struct type *value_type;
  1394.      int gcc_p;
  1395.      /*ARGSUSED*/
  1396. {
  1397.   register enum type_code code = TYPE_CODE (value_type);
  1398.  
  1399.   if (code == TYPE_CODE_ERROR)
  1400.     error ("Function return type unknown.");
  1401.  
  1402.   if (code == TYPE_CODE_STRUCT ||
  1403.       code == TYPE_CODE_UNION ||
  1404.       code == TYPE_CODE_ARRAY)
  1405.     return USE_STRUCT_CONVENTION (gcc_p, value_type);
  1406.  
  1407.   return 0;
  1408. }
  1409.  
  1410. /* Store VAL so it will be returned if a function returns now.
  1411.    Does not verify that VAL's type matches what the current
  1412.    function wants to return.  */
  1413.  
  1414. void
  1415. set_return_value (val)
  1416.      value val;
  1417. {
  1418.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  1419.   double dbuf;
  1420.   LONGEST lbuf;
  1421.  
  1422.   if (code == TYPE_CODE_ERROR)
  1423.     error ("Function return type unknown.");
  1424.  
  1425.   if (   code == TYPE_CODE_STRUCT
  1426.       || code == TYPE_CODE_UNION)    /* FIXME, implement struct return.  */
  1427.     error ("GDB does not support specifying a struct or union return value.");
  1428.  
  1429.   /* FIXME, this is bogus.  We don't know what the return conventions
  1430.      are, or how values should be promoted.... */
  1431.   if (code == TYPE_CODE_FLT)
  1432.     {
  1433.       dbuf = value_as_double (val);
  1434.  
  1435.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&dbuf);
  1436.     }
  1437.   else
  1438.     {
  1439.       lbuf = value_as_long (val);
  1440.       STORE_RETURN_VALUE (VALUE_TYPE (val), (char *)&lbuf);
  1441.     }
  1442. }
  1443.  
  1444. void
  1445. _initialize_values ()
  1446. {
  1447.   add_cmd ("convenience", no_class, show_convenience,
  1448.         "Debugger convenience (\"$foo\") variables.\n\
  1449. These variables are created when you assign them values;\n\
  1450. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1451. A few convenience variables are given values automatically:\n\
  1452. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1453. \"$__\" holds the contents of the last address examined with \"x\".",
  1454.        &showlist);
  1455.  
  1456.   add_cmd ("values", no_class, show_values,
  1457.        "Elements of value history around item number IDX (or last ten).",
  1458.        &showlist);
  1459. }
  1460.